/* Copyright 2007 Gearbox Software - http://www.gearboxsoft.com
 
 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at
 
     http:www.apache.org/licenses/LICENSE-2.0
 
 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.*/
namespace GearBlog.Web.Utils
{
	using System.Collections;
	using System.Collections.Specialized;
	using Castle.MonoRail.Framework;
	using GearBlog.Web.ConfigSections;

	public class FormsAuthenticationAgent:AuthenticationAgentBase
	{
		/// <summary>
		/// Redirect to Login Screen. Form Authentication will rely on login controller to do the actual logon, so this method would always return false
		/// </summary>
		/// <param name="controller"></param>
		/// <param name="returnUrl"></param>
		/// <returns></returns>
		public override bool AttemptLogin(Controller controller, string returnUrl)
		{
			
			MembershipManagerSection membershipManagerSection = MembershipManagerSection.GetConfig();

			OrderedDictionary parameters = new OrderedDictionary();
			parameters.Add("returnUrl", controller.Context.Url);
			LinkElement loginLink = membershipManagerSection.Links["Login"];

			string url =
				controller.UrlBuilder.BuildUrl(controller.Context.UrlInfo, loginLink.Area, loginLink.Controller, loginLink.Action,
											   parameters);

			controller.Flash["url"] = url;
			if (RequestUtil.IsAjax(controller.Context.Request))
				controller.Redirect("Ajax", "JSRedirect");
			else
				controller.Redirect(url);
			return false;
		}
		public override void ShowAccessDenied(Controller controller)
		{
			MembershipManagerSection membershipManagerSection = MembershipManagerSection.GetConfig();
			LinkElement accessDeniedLink = membershipManagerSection.Links["AccessDenied"];

			controller.Redirect(accessDeniedLink.Area, accessDeniedLink.Controller, accessDeniedLink.Action, (IDictionary)null);

		}
	}
}
